/**
 * Created by luckyjing on 16/3/27.
 */
var Rent = require('mongoose').model('Rent');
var Tool = require('mongoose').model('Tool');
var User = require('mongoose').model('User');
var Util = require('../util/util');
var rentStatus = require('../constant/rentStatus');
var userType = require('../constant/userType');
var toolType = require('../constant/tool');
var toolController = require('./tool.server.controller');
exports.list = function (req, res) {
    var param = {};
    // 如果不是管理员的话,只能看到自己的申请信息
    if (req.user.type = userType[ 0 ].key) {
        var user = new User(req.user);
        param = { author: user }
    }
    Rent.find(param).populate('tool user', '-id -salt -password -__v -created').sort({ '_id': -1 }).exec((err, rents)=> {
        if (err) {
            return res.json(Util.Info.ERROR);
        } else {
            res.json(rents);
        }
    });
};
exports.addRent = function (req, res) {
    if (req.body.num <= 0) {
        return res.json(Util.Info.PARAM_ERROR);
    }
    // 管理员将手动携带借阅者信息
    var rent = new Rent(req.body);
    rent.tool = req.body.id;
    if (req.user.type != userType[ 2 ].key) {
        //如果不是管理员,那么需要追加
        rent.user = req.user;
    }
    toolController.listById(rent.tool, (err, tool)=> {
        if (err) {
            return res.json(Util.Info.ERROR);
        } else {
            if (!tool) {
                return res.json(Util.Info.PARAM_ERROR)
            }
            if (rent.num > tool.left) {
                return res.json(Util.Info.PARAM_ERROR);
            }
            if (req.user.type != userType[ 2 ].key) {
                //如果不是管理员,那么需要更改状态
                if (tool.type == toolType[ 0 ].key) {
                    //普通工具
                    rent.status = 0;
                } else {
                    //昂贵工具
                    rent.status = 1;
                }
            } else {
                rent.status = 0;
            }
            rent.save((err)=> {
                if (err) {
                    return res.json(Util.Info.ERROR);
                } else {
                    if (tool.type == toolType[ 0 ].key || req.user.type == userType[ 2 ].key) {
                        //如果是申请一般工具,则立即修改工具数量
                        changeNumber(tool._id, {
                            left: -rent.num,
                            rent: rent.num
                        }).then(
                            (success)=>res.json(success),
                            (err)=>res.json(err)
                        );
                    } else {
                            // 是贵重工具,不用修改数据,在审核通过时才去修改工具数量
                            res.json(Util.Info.SUCCESS);
                    }
                }
            })
        }
    });

};
function changeNumber (id, para) {
    return new Promise((resolve, reject)=> {
        Tool.findByIdAndUpdate(id, {
            $inc: para
        }, (err)=> {
            if (err) {
                reject(Util.Info.ERROR);
            } else {
                resolve(Util.Info.SUCCESS);
            }
        });
    });
}
exports.changeRent = function (req, res) {
    var id = req.params.id;
    var status = req.body.status;
    Rent.findById(id).populate('tool user', '-id -salt -password -__v -created').exec((err, rent)=> {
        if (!rent) {
            return res.json(Util.Info.PARAM_ERROR);
        }
        if (err) {
            return res.json(Util.Info.ERROR);
        } else {
            if (status == rentStatus[ 2 ].key) {
                console.log(rent.tool);
                changeNumber(rent.tool.id, {
                    left: rent.num,
                    rent: -rent.num
                }).then(
                    (success)=> {
                        rent.status == status;
                        rent.save();
                        res.json(success);
                    },
                    (err)=>res.json(err)
                );
            } else if (req.body.status == rentStatus[ 3 ].key) {
                changeNumber(rent.tool.id, {
                    rent: -rent.num
                }).then(
                    (success)=> {
                        rent.status == status;
                        rent.save();
                        res.json(success);
                    },
                    (err)=>res.json(err)
                );
            }
        }
    });

};
exports.checkRent = function (req, res) {
    var id = req.params.id;
    Rent.findById(id).populate('tool user', '-id -salt -password -__v -created').exec((err, rent)=> {
        if (!rent) {
            return res.json(Util.Info.PARAM_ERROR);
        }
        if (err) {
            return res.json(Util.Info.ERROR);
        } else {
            changeNumber(rent.tool.id, {
                left: -rent.num,
                rent: rent.num
            }).then(
                success=> {
                    rent.status == 1;
                    rent.save();
                    res.json(success);
                },
                err=>res.json(err)
            );
        }
    });
};